package com.charger.PromoteOperations.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.charger.PromoteOperations.domain.Client;
import com.charger.PromoteOperations.domain.ClientNewclient;
import com.charger.PromoteOperations.domain.Newclient;
import com.charger.PromoteOperations.service.ClientNewclientService;
import com.charger.PromoteOperations.service.IClientService;
import com.charger.PromoteOperations.service.NewclientService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.charger.common.annotation.Log;
import com.charger.common.core.controller.BaseController;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.enums.BusinessType;
import com.charger.PromoteOperations.domain.Invite;
import com.charger.PromoteOperations.service.IInviteService;
import com.charger.common.utils.poi.ExcelUtil;
import com.charger.common.core.page.TableDataInfo;

/**
 * 邀请管理Controller
 * 
 * @author ruoyi
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/invite/invite")
public class InviteController extends BaseController
{
    @Autowired
    private IInviteService inviteService;
    @Autowired
    private IClientService clientService;
    @Autowired
    private ClientNewclientService clientNewclientService;
    @Autowired
    private NewclientService newclientService;

    /**
     * 查询邀请管理列表
     */
    @PreAuthorize("@ss.hasPermi('invite:invite:list')")
    @GetMapping("/list")
    public TableDataInfo list(Invite invite)
    {
        startPage();
        List<Invite> list = inviteService.selectInviteList(invite);
        for (Invite inviteList : list){
            Long clientId = inviteList.getClientId();
            Client client = clientService.selectClientById(clientId);
            inviteList.setClient(client);
            QueryWrapper<ClientNewclient> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("client_id",clientId);
            List<ClientNewclient> clientNewclients = clientNewclientService.list(queryWrapper);
            List<Newclient> newclients = new ArrayList<>();
            for (ClientNewclient clientNewclient:clientNewclients){
                boolean equals = clientNewclient.getIsNewinivit().equals("1");
                if (equals){
                    Long newclientId = clientNewclient.getNewclientId();
                    Newclient byId = newclientService.getById(newclientId);
                    newclients.add(byId);
                }
            }
            inviteList.setNewclients(newclients);
        }
        return getDataTable(list);
    }

    /**
     * 导出邀请管理列表
     */
    @PreAuthorize("@ss.hasPermi('invite:invite:export')")
    @Log(title = "邀请管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Invite invite)
    {
        List<Invite> list = inviteService.selectInviteList(invite);
        ExcelUtil<Invite> util = new ExcelUtil<Invite>(Invite.class);
        util.exportExcel(response, list, "邀请管理数据");
    }

    /**
     * 获取邀请管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('invite:invite:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Invite invite = inviteService.selectInviteById(id);
        Long clientId = invite.getClientId();
        Client client = clientService.selectClientById(clientId);
        invite.setClient(client);
        QueryWrapper<ClientNewclient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("client_id",clientId);
        List<ClientNewclient> clientNewclients = clientNewclientService.list(queryWrapper);
        List<Newclient> newclients = new ArrayList<>();
        for (ClientNewclient clientNewclient:clientNewclients){
            boolean equals = clientNewclient.getIsNewinivit().equals("0");
            if (equals){
                Long newclientId = clientNewclient.getNewclientId();
                Newclient byId = newclientService.getById(newclientId);
                newclients.add(byId);
            }

        }
        invite.setNewclients(newclients);
        return AjaxResult.success(invite);
    }


    /**
     * 新增邀请管理
     */
    @PreAuthorize("@ss.hasPermi('invite:invite:add')")
    @Log(title = "邀请管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Invite invite)
    {
        return toAjax(inviteService.insertInvite(invite));
    }

    /**
     * 修改邀请管理
     */
    @PreAuthorize("@ss.hasPermi('invite:invite:edit')")
    @Log(title = "邀请管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Invite invite)
    {
        int i = inviteService.updateInvite(invite);
        List<Long> newInviteId = invite.getNewInviteId();
        if (newInviteId != null){
            for (Long newInviteId1 : newInviteId){
                ClientNewclient clientNewclient = new ClientNewclient();
                clientNewclient.setClientId(invite.getClientId());
                clientNewclient.setNewclientId(newInviteId1);
                int i1 = clientNewclientService.updateClientNewclient(clientNewclient);
//                将新增的被邀请人添加到客户列表，将字段一一匹配添加
//                通过新增的id查询新增的邀请人信息
                Newclient newclient = newclientService.getById(newInviteId1);
//                创建一个新的客户对象，将新增邀请人的信息赋值给这个对象
                Client client = new Client();
                client.setName(newclient.getName());
                client.setUsername(newclient.getUsername());
                client.setPhone(newclient.getPhone());
                client.setSex(newclient.getSex());
                client.setSpawnTime(newclient.getSpawntime());
                client.setIdentificationNumber(newclient.getIdentificationnumber());
                client.setInvestors(newclient.getInvestors());
                client.setChargeCard(newclient.getChargecard());
                client.setIntegral(newclient.getIntegral());
                client.setAuthentication(newclient.getAuthentication());
                client.setImg(newclient.getImg());
                client.setAddress(newclient.getAddress());
                int i2 = clientService.insertClient(client);
            }
        }
        return toAjax(i);
    }

    /**
     * 删除邀请管理
     */
    @PreAuthorize("@ss.hasPermi('invite:invite:remove')")
    @Log(title = "邀请管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(inviteService.deleteInviteByIds(ids));
    }
}
